package com.iwunu.iot.common.adapter;

import com.alibaba.fastjson.JSONObject;
import com.iwunu.iot.common.constant.CodeEnum;

public class MessageConversionAdapter {
	private final static String startWith ="68";
	private final static String endWith ="0016";
	private final static String inputService ="61";
	private final static Integer index = 1;
	private final static Integer sendType = 1;
	private final static Integer deviceType = 1;
	private final static Integer deviceId = 4;
	private final static Integer random= 2;
	private final static Integer serviceType = 2;
	private final static Integer timestamp = 4;
	private final static Integer count = 2;
	private final static Integer flag = 1;
	private final static Integer dataLength = 1;
	private final static Integer multiple = 2;
	private final static Integer dataTypelength = 1;
	
	/**
	 * *获取设备ID
	 * @param msg
	 * @return
	 */
	public static String getDeviceId(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getBaseLength(), getDeviceIdEnd());
		}
		return "";
	}

	/**
	 * *获取随机数
	 * @param msg
	 * @return
	 */
	public static String getRandom(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getDeviceIdEnd(), getRandomEnd());
		}
		return "";
	}
	/**
	 * 	获取服务类型
	 * @param msg
	 * @return
	 */
	public static String getServiceType(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getRandomEnd(),getServiceTypeEnd());
		}
		return "";
	}
	/**
	 * 	获取硬件发过来的时间戳
	 * @param msg
	 * @return
	 */
	public static String getTimestamp(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getServiceTypeEnd(), getTimestampEnd());
		}
		return "";
	}
	/**
	 * 	获取数据总长度
	 * @param msg
	 * @return
	 */
	public static Integer getCount(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			String length =msg.substring(getTimestampEnd(), getCountEnd());
			return Integer.parseInt(length);
		}
		return null;
	}
	/**
	 * 	获取三元组的FLAG（小类型）
	 * @param msg
	 * @return
	 */
	public static Integer getFlag(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return Integer.parseInt(msg.substring(getCountEnd(), getFlagEnd()));
		}
		return 0;
	}
	/**
	 * 	获取传输数据
	 * @param msg
	 * @return
	 */
	public static String getData(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			String data = msg.substring(getDataLengthEnd(),getDataLengthEnd()+getDataLength(msg));
			return data;
		}
		return "";
	}
	/**
	 * 	获取设备编号
	 * @param msg
	 * @return
	 */
	public static String getDeviceCode(String msg) {
		if(!msg.startsWith(startWith)) {
			return "";
		}
		if(getFlag(msg).equals(inputService)) {
			return getData(msg);
		}
		return "";
	}
	/**
	 *	 替换返回数据
	 * @param msg
	 * @param newChar
	 * @return
	 */
	public static String replaceData(String msg,String newChar) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			StringBuffer msgbuf = new StringBuffer(msg);
			return msgbuf.replace(getDataLengthEnd(),getDataEnd(msg),newChar).toString();
		}
		return "";
	}
	
	public static Integer getBaseLength() {
		return (index+sendType+deviceType)*multiple;
	}
	public static Integer getDeviceIdEnd() {
		return getBaseLength()+deviceId*multiple;
	}
	public static Integer getRandomEnd() {
		return getDeviceIdEnd()+random*multiple;
	}
	public static Integer getServiceTypeEnd() {
		return  getRandomEnd() + serviceType*multiple;
	}
	public static Integer getTimestampEnd() {
		return getServiceTypeEnd()+timestamp*multiple;
	}
	public static Integer getCountEnd() {
		return	getTimestampEnd()+count*multiple;
	}
	public static Integer getFlagEnd() {
		return  getCountEnd() + flag*multiple;
	}	
	public static Integer getDataLengthEnd() {
		return getFlagEnd()+dataLength*multiple;
	}
	public static Integer getDataLength(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			String length = msg.substring(getFlagEnd(), getDataLengthEnd());
			return Integer.parseInt(length,16)*multiple;
		}
		return null;
	}
	public static Integer getDataEnd(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			return	getDataLengthEnd()+getDataLength(msg);
		}
		return null;
	}
	public static JSONObject getSingleJson(String data ,String deviceId) {
		JSONObject result = new JSONObject();
		String userId01 = data.substring(0,getUserIdLength(data)*multiple);
		String type = data.substring(getUserIdLength(data)*multiple, data.length());
		result.put("smartlockSN", deviceId);
		result.put("unlockMode", CodeEnum.unlockMode_simple.getCode());
		result.put("userId01", Integer.parseInt(userId01,16 ));
		result.put("identityType01", catchType(type));
		return result;
	}
	public static JSONObject getMoreJson(String data,String deviceId) {
		JSONObject result = new JSONObject();
		String userId01 = data.substring(0,getUserIdLengthMore(data)*multiple);
		String type01 = data.substring(getUserIdLengthMore(data)*multiple, (getUserIdLengthMore(data)+1)*multiple);
		int user02start = (getUserIdLengthMore(data)+dataTypelength)*multiple;
		int user02end =user02start + getUserIdLengthMore(data)*multiple;
		int type02end = user02end + dataTypelength*multiple;
		String userId02 = data.substring(user02start,user02end);
		String type02 =  data.substring(user02end, type02end);
		result.put("smartlockSN", deviceId);
		result.put("unlockMode", CodeEnum.unlockMode_double.getCode());
		result.put("userId01", Integer.parseInt(userId01,16 ));
		result.put("identityType01", catchType(type01));
		result.put("userId02", Integer.parseInt(userId02,16 ));
		result.put("identityType02", catchType(type02));		
		return result;
	}
	/**
	 * userId01
	 * @param data
	 * @return
	 */
	public static String getUserId01(String data) {
		
		return data.substring(0,getUserIdLength(data)*multiple);
		
	}
	public static Integer getUserIdLength(String data) {
		Integer userIdLength = data.length()-1;
		return userIdLength;
	}
	public static Integer getUserIdLengthMore(String data) {
		Integer userIdLength = data.length()/2-1;
		return userIdLength;
	}
	public static Integer catchType(String type) {
		Integer result = null;
		switch(type) {
		case "01":
			result = CodeEnum.identityType_fingler.getCode();
			break;
		case "02":
			result = CodeEnum.identityType_password.getCode();
			break;
		case "03":
			result = CodeEnum.identityType_card.getCode();
			break;
		case "04":
			result = CodeEnum.identityType_face.getCode();
			break;
		case "05":
			result = CodeEnum.identityType_id.getCode();
			break;
		case "0A":
			result = CodeEnum.identityType_face.getCode();
			break;
		case "0C":
			result = CodeEnum.identityType_id.getCode();
			break;
		}
		return result;
	}
	public static Integer catchErrorType(String type) {
		Integer result = CodeEnum.identityType_fingler.getCode();
		switch(type) {
		case "03":
			result = CodeEnum.identityType_card.getCode();
			break;
		case "04":
			result = CodeEnum.identityType_password.getCode();
			break;
		case "11":
			result = CodeEnum.identityType_face.getCode();
			break;
		case "12":
			result = CodeEnum.identityType_id.getCode();
			break;
		case "05":
			result = CodeEnum.eventType_lock.getCode();
			break;
		case "30":
			result = CodeEnum.eventType_unlock.getCode();	
			break;
		
		}
		return result;
	}
	public static void main(String[] args) {
		String data = getData("680301000000018E3100015F43D7A6000B62030001010016");
		String userId01 = data.substring(0,getUserIdLength(data)*multiple);
		String t1 = data.substring(getUserIdLength(data)*multiple, (getUserIdLength(data)+1)*multiple);
		System.out.println(Integer.parseInt("000B", 16));
	}
}
